<template>
  <BaseMainSlot>
    <!-- 这页面 -->
    <template v-slot:zhactbar>
      <ActionBar
        :page-no="102"
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :has-edit="false"
        :has-del="false"
        :has-save="false"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel"
        @del="del"
      />
    </template>
    <template v-slot:boxleft>
      <div class="zhtabletitle">已有生产作业</div>
      <el-table
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="85%"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="agriculturalWorkTypeName" label="生产作业名称" />
        <el-table-column width="200" label="操作">
          <template
            v-if="scope.row.agriculturalWorkTypeId"
            slot-scope="scope"
          >
            <el-button
              v-if="judgePermission(102,'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row.agriculturalWorkTypeNo)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:boxright>
      <div class="zhtabletitle">所有生产作业</div>
      <el-table
        v-loading="loadingtwo"
        highlight-current-row
        :data="tableDatatwo"
        stripe
        border
        height="85%"
        @selection-change="handleSelectionChangetwo"
      >
        <el-table-column type="selection" width="55" :selectable="selectable" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="svcClassName" label="生产作业名称" />
        <!-- <el-table-column prop="description" label="农业类型描述" /> -->
      </el-table>
      <Pagination
        :page-size="pageSizetwo"
        :current-page="currentPagetwo"
        :total-size="totalSizetwo"
        @changePageSize="changePageSizetwo"
        @changeCurrentPage="changeCurrentPagetwo"
      />
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar/index";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/work-type-management";
import BaseMainSlot from "@/components/BaseMainSlot/basezuoyou.vue";
import uploadimagesVue from "@/components/uploadimage/uploadimages.vue";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from 'vuex';
import axios from "axios";
// import { getToken } from '@/utils/auth';
export default {
  name: "AgriculturalTypeManagement",
  components: { ActionBar, Pagination, BaseMainSlot, uploadimagesVue },
  mixins: [mymixin],
  data() {
    return {
      // ----表格二号----
      loadingtwo: true,
      // 当前页
      currentPagetwo: 1,
      // 每页的数据条数
      pageSizetwo: 10,
      // 总条数
      totalSizetwo: 0,
      // 表格二号数据
      tableDatatwo: [],
      // 是否能输入
      // -------
      canInput: false,
      rules: {},
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      nowselectedList: [],
      farmingTypeNo: undefined,
      dialogImgFormVisible: false,
      InImgUrl: '',
      productPicture: ''
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.getTableDataListone();
    // 获取表格数据
    this.getTableDataListtwo();
  },
  mounted() {},
  methods: {
    // 获取当前拥有类型表格
    getTableDataListone() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          organizationNo: sessionStorage.organizationNo,
          farmingTypeNo: sessionStorage.farmingTypeNo,
        }
      };
      nowpagereq.queryPageByCondition(params).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("当前农业类型表格获取失败");
          return
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
        this.addActive = false;
      });
    },
    // 获取所有农业类型表格
    async getTableDataListtwo() {
      this.loadingtwo = true;

      let params = {
        page: this.currentPagetwo,
        size: this.pageSizetwo,
        entity: {
          businessType: 'AGRICULTURE_WORK_TYPE',
          farming: 'PLANT',
        }
      };
      const { data: res } = await axios({
        method: "post",
        url: this.$baseurl.webServiceUrl + 'v3/config/serviceClass/queryPageList',
        headers: {
          databaseName: 'DB_TDW1_PUBLIC'
        },
        data: params
      })
      this.loadingtwo = false;
      if (res.code !== 1) {
        this.$message.error("所有生产作业表格获取失败");
        return
      }
      let data = res.data;
      // 赋值总条数
      this.totalSizetwo = data.totalElements;
      // 赋值数组
      this.tableDatatwo = data.content;
      if (this.tableDatatwo.length < this.pageSizetwo) {
        let havline = this.tableDatatwo.length % this.pageSizetwo;
        if (havline < this.pageSizetwo) {
          const remain = this.pageSizetwo - havline;
          for (var i = 0; i < remain; i++) {
            this.tableDatatwo.push({
              index: i + this.tableDatatwo.length - 1
            });
          }
        }
      }
      this.resetActionBar();
      this.addActive = false;
    },
    search() {
      this.currentPage = 1;
      this.getTableDataListone();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataListone();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataListone();
    },
    changePageSizetwo(pageSize) {
      this.pageSizetwo = pageSize;
      this.getTableDataListtwo();
    },
    changeCurrentPagetwo(currentPage) {
      this.currentPagetwo = currentPage;
      this.getTableDataListtwo();
    },
    handleSelectionChange(list) {
      this.nowselectedList = [];
      this.nowselectedList = list.map((item) => {
        return item.id;
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) { this.delActive = true; } else this.delActive = false;
    },
    handleEdit(row) {
      if (row) {
        this.formData = { ...row }
      }
    },
    handleSelectionChangetwo(list) {
      this.nowselectedList = [];
      this.nowselectedList = list.map((item) => {
        return {
          agriculturalWorkTypeNo: item.svcClassCode,
          agriculturalWorkTypeName: item.svcClassName,
          organizationNo: sessionStorage.getItem("organizationNo"),
          farmingTypeNo: sessionStorage.getItem('farmingTypeNo'),
        };
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) { this.addActive = true; } else this.addActive = false;
    },
    handleDelete(id) {
      this.$confirm("是否删除生产作业？", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonClass: "btnFalses",
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      }).then(() => {
        nowpagereq.del([id]).then((res) => {
          if (res.code === 1) {
            this.$message.success("删除成功");
            this.getTableDataListone();
          } else {
            this.$message.error("删除失败");
          }
        });
      }).catch((action) => {
      });
    },
    add () {
      this.$confirm("是否添加生产作业？", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonClass: "btnFalses",
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      }).then(() => {
        nowpagereq.addBatch(this.nowselectedList).then((res) => {
          if (res.code === 1) {
            this.$message.success("添加成功");
            this.getTableDataListone();
          } else {
            this.$message.error(res.msg || "添加失败");
          }
        });
      }).catch((action) => {
        
      });
    },
    selectable (row) {
      return !('index' in row)
    },
  }
};
</script>
<style lang="scss" scoped>
.zhtabletitle {
  color: #31f7dd;
  margin-bottom: 10px;
}
// ::v-deep .el-message-box__wrapper {
//       background: red !important;
//   .el-message-box {
//     .btnFalses {
//       background: red !important;
//     }
//   }
// }
</style>
